/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package se.superduperbudget.central.ws.user;

import java.util.GregorianCalendar;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.ws.rs.PathParam;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import se.superduperbudget.central.core.EMF;
import se.superduperbudget.central.db.SuperDuperTag;
import se.superduperbudget.central.db.SuperDuperTransaction;
import se.superduperbudget.central.jaxbbindings.Tag;
import se.superduperbudget.central.jaxbbindings.TagList;
import se.superduperbudget.central.jaxbbindings.TransactionList;
import se.superduperbudget.central.jaxbbindings.UserTransaction;

/**
 * REST Web Service
 *
 * @author Emil
 */

@Stateless
@Path("/user")
public class RecentTransactions {
    /** Creates a new instance of UserResource */
    public RecentTransactions() {
    }
    
    @GET
    @Path("recenttransactions/{uuid}")
    @Produces({"application/xml", "application/json"})
    public TransactionList getXml(@PathParam("uuid") String userUuid) throws DatatypeConfigurationException {
        String query = 
                "select t, b.name from SuperDuperUser u "
                + "join u.userBanks ub "
                + "join ub.accounts a "
                + "join a.transactions t "
                + "join ub.bank b "
                + "where u.uuid = :uuid "
                + "order by t.date desc";
        
        EntityManager em = EMF.get().createEntityManager();
        Query q = em.createQuery(query);
        q.setParameter("uuid", userUuid);
        q.setFirstResult(0);
        q.setMaxResults(15);
        
        TransactionList res = new TransactionList();
        List<UserTransaction> list = res.getTransaction();
        
        for (Object[] row : (List<Object[]>)q.getResultList()) {
            SuperDuperTransaction sdt = (SuperDuperTransaction) row[0];
            String bankName = (String) row[1];
            
            UserTransaction ut = new UserTransaction();
            GregorianCalendar c = new GregorianCalendar();
            c.setTime(sdt.getDate());

            ut.setBank(bankName);
            ut.setDate(DatatypeFactory.newInstance().newXMLGregorianCalendar(c));
            
            TagList tl = new TagList();
            tl.getTag().clear();    // TODO: Tags
            ut.setTaglist(tl);
            
            query = "select t from SuperDuperTransaction tr "
                    + "join tr.tags t "
                    + "where tr.id = :id";
            
            TypedQuery<SuperDuperTag> tq = em.createQuery(query, SuperDuperTag.class);
            tq.setParameter("id", sdt.getId());
            for (SuperDuperTag tag : tq.getResultList()) {
                Tag tag2 = new Tag();
                tag2.setId(tag.getId());
                tag2.setName(tag.getName());
                tl.getTag().add(tag2);
            }
            
            ut.setId(sdt.getId());     
            ut.setTitle(sdt.getText());
            ut.setDiff(sdt.getDiff());
            
            list.add(ut);
        }
        
        em.close();
        
        return res;
    }
}
